<?php

class DB_BLOG extends ORM_Table {
    
    static protected $table_name = 'blog';
    
    static public function getTableName($params = null) {
        return self::$table_name;
    }
    
    /**
     * 根据微博ID获取博客的评论
     * @param type $weibo_id
     * @return array 
     */
    static function getBlogIds($weibo_id){
        if (!$conn_s = self::$conn) {
            $conn_s = parent::$conn;
        }
        $re = array();
        $id = array();
        $conn = ConnectionManager::getConn($conn_s);
        $rs = $conn->query("SELECT idstr FROM {#prefix#}".self::$table_name." WHERE weibo_id = ? and comments_count > ?", array($weibo_id, 0))->fetchAll();
        foreach ($rs as $k => $v) {
            if(count($re)==50){
                $id[]   = $re;
                $re     = array();
            }
            $re[] = $v['idstr'];
        }
        return $id;
    }
    
    /**
     * 存储blog
     * @param type $weibo_id
     * @param type $array
     * @return DB_BLOG 
     */
    static function store($weibo_id, $array){
        
            $blog   = ORM_Finder::findOne(DB_BLOG, array('idstr'=>$array['idstr']));
            //更新用户信息
            DB_WEIBO::batch_save(array($array['user']));

            if(!$blog){
                $blog   = new DB_BLOG();
                $blog->setWeiboId($weibo_id);
                $blog->setMid($array['mid']);
                $blog->setIdstr($array['idstr']);
                $blog->setText($array['text']);
                $blog->setSource($array['source']);
                $blog->setFavorited((int)$array['favorited']);
                $blog->setTruncated((int)$array['truncated']);
                $blog->setGeo(serialize($array['geo']));
                $blog->setRepostsCount($array['reposts_count']);
                $blog->setCommentsCount($array['comments_count']);
                $blog->setUsers(serialize($array['user']));
                $blog->setCreatedAt(date('Y-m-d H:i:s', strtotime($array['created_at'])));
                
                if(isset($array['thumbnail_pic'])){
                    $blog->setThumbnailPic($array['thumbnail_pic']);
                }
                
                if(isset($array['bmiddle_pic'])){
                    $blog->setBmiddlePic($array['bmiddle_pic']);
                }
                
                if(isset($array['original_pic'])){
                    $blog->setOriginalPic($array['original_pic']);
                }

                //是转发的
                if(isset($array['retweeted_status'])){
                    $blog->setRetweetedStatus($array['retweeted_status']['idstr']);
                }
                
                $blog->save();
            }else{
                $blog->setRepostsCount($array['reposts_count']);
                $blog->setCommentsCount($array['comments_count']);
                $blog->save();
            }
            return $blog;
    }
}

?>
